<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\parkingService;
use app\service\apiService;
use think\Db;
use think\db\Where;
use think\facade\View;

class Parking extends Common{
    //停车场查询
    public function parkingList(){
        //查询当前ID用户类型
        $com = $this->pidTokenId();
        $com['parking_id']=implode(",",$com['parking_id']);
        $whe['id'] = array('in',$com['parking_id']);
        //根据当前账号下的所有区域查询所有停车场
        $parkinglist = (new parkingService())->parkingList($whe);
        //查询对应物业名称
        foreach($parkinglist as $lis){

            
            $list[] = $lis;
        }
        $number = count($parkinglist);
        View::assign('number',$number);
        if(empty($list)){
            View::assign('list',$parkinglist);
        }else{
            View::assign('list',$list);
        }
        return View::fetch('parking_list');
    }
    //停车场添加
    public function parkingAdd(){


        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断账号名称是否重复
            $info = (new parkingService())->parkingName($data['parkingName']);
            if($info){
                $this->error('停车场已存在');
            }
            
            //插入数据到数据库
            $parkingid = (new parkingService())->parkingAdd($data);
            if($parkingid){
                // $parkid = string($parkingid);
                if(strlen($parkingid) == 1){
                    $zero = '0000';
                }elseif(strlen($parkingid) == 2){
                    $zero = '000';
                }elseif(strlen($parkingid) == 3){
                    $zero = '00';
                }elseif(strlen($parkingid) == 4){
                    $zero = '0';
                }
                $info['parkingNo'] = $data['community_id'].$zero.$parkingid;
                $info['id'] = $parkingid;
                $result = (new parkingService())->parkingEdit($info);
                $this->success('添加停车场成功',url('parkingAdd'));
            }else{
                $this->error('添加停车场失败',url('parkingAdd'));
            }
        }
        return View::fetch("parking_add");
    }
    //停车场修改
    public function parkingEdit(){
        //查询当前ID用户类型
        $com = $this->pidTokenId();
        $where=new Where();
        $where['id'] = array('in',$com['community_id']);
        $communitylist = (new parkingService())->communityList($where);
        $this->assign('communitylist',$communitylist);

        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('停车场不存在');
            }
            $info = (new parkingService())->parkingId($id);
            $this->assign('info',$info);
            return $this->fetch('parking_edit');
        }else{
            $data = request()->post();
            $result = (new parkingService())->parkingEdit($data);
            if($result){
                $this->success('修改停车场成功',url('parkingList'));
            }else{
                $this->error('修改停车场失败');
            }
        }
    }
    //停车场删除
    public function parkingDel(){
        $id = request()->param('id');
        $validate = $this->validate(request()->param(),[
            'id'	=>	'require|number'
        ]);
        if($validate !== true){
            $this->error($validate);
        }
        //判断是否存在该数据
        $info = (new parkingService())->parkingId($id);
        if(empty($info)){
            $this->error('该停车场不存在');
        }
        $client=Db::table("parking_client")->where("parkingNo",$info["parkingNo"])->find();
        if(!empty($client)){
            $this->error('请先删除该车场下的客户端');
        }
        
        $result =  (new parkingService())->parkingDel($id);
        if($result){
            $this->success('删除停车场成功');
        }else{
            $this->success('删除停车场失败');
        }
    }

    
	
	public function parkingprinting(){
		 if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //将数据插入数据库
            $result = (new parkingService())->parkingEdit($data);
            //判断数据是否插入成功
            if($result){
                $this->success('配置参数成功',url('parkinglist'));
            }else{
                $this->error('配置参数失败',url('parkinglist'));
            }
        }else{
			if (!request()->has('id') || empty(request()->param('id'))) {
                $this->error('错误');
            }
            $id = request()->param('id');
            $info = (new parkingService())->parkingId($id);
            $this->assign('info',$info);
            if($info){
                return $this->fetch('parking_printing');
            }else{
                return json(['info' => '小区信息错误']);
            }
        }
	}

	
	
	public function clientList(){
	    //查询当前ID用户类型
        $parkingNo = $this->getParkingNo();
        
        $clientList=Db::table('parking_client')
                ->alias('a')
                ->join('parkinglotinfo b ','a.parkingNo= b.parkingNo')
                ->leftJoin("client_login c",'a.parkingNo=c.parkingNo and a.clientNo=c.clientNo')
                ->field("a.*,b.parkingName,c.connect_status")
                ->select();
        $number = 1;
        $this->assign('number',$number);
       
        $this->assign('list',$clientList);
        return $this->fetch('client_list');
	}
	
	public function clientAdd(){
	    if(request()->isPost()){
	        $data=request()->post();
	        $res=Db::table("parking_client")
	                ->where("parkingNo",$data["parkingNo"])
	                ->where("clientNo",$data["clientNo"])
	                ->find();
	        
	        if(!empty($res)){
	            $this->error("同一车场客户端不能重复");
	        }
	        
	        $re=Db::table("parking_client")->insert($data);
	        if($re){
	            $this->success("添加成功");
	        }else{
	            $this->success("添加失败");
	        }
	    }else{
	         //查询当前ID用户类型
            $com = $this->pidTokenId();
            $whe=new Where();
            $whe['id'] = array('in',$com['community_id']);
            $communitylist = (new parkingService())->communityList($whe);
            $this->assign('communitylist',$communitylist);
            $community_id = $com['community_id'];
            //根据当前账号下的所有区域查询所有停车场
            $parkinglist = (new parkingService())->parkingList($community_id);
            $this->assign("parkinglist",$parkinglist);
            return $this->fetch("client_add");
	    }
	    
        
	}
    public function clientEdit(){
	    if(request()->isPost()){
            $data=request()->post();
            $res=Db::table("parking_client")->update($data);
            if($res){
                return "配置成功";
            }else{
                return "配置失败";
            }
        }else{
	        $id=request()->get("id");
            $info=Db::table("parking_client")->where("id",$id)->find();
            if(empty($info)){
                return "客户端不存在";
            }
            $this->assign("info",$info);
            return $this->fetch("client_edit");
        }
    }
	public function clientDel(){
	    $id=request()->get("id");
	    $caminfo=Db::table("parking_camera")->where("parking_client_id",$id)->find();
	    if(!empty($caminfo)){
	        $this->error("请先删除该客户端下的设备");
	    }
        $res=Db::table("parking_client")->where("id",$id)->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->success('删除失败');
        }
        
	}
	 public function cameraList(){
        $parkingNo = $this->getParkingNo();
        $page = request()->has('page') ? request()->get('page') : 1;
        $where=array();
        $list = Db::name('parking_camera')->where('parkingNo','in',$parkingNo)->where($where)->order('id desc')->paginate(100, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);
        $number =  Db::name('parking_camera')->where('parkingNo','in',$parkingNo)->where($where)->count();
        $this->assign("list",$list);
        $this->assign("number",$number);
        return $this->fetch("camera_list");
    }
    public function cameraAdd(){
       if(request()->isPost()){
           $data=request()->post();
           //
           $re=Db::table("parking_camera")
                    ->where("parkingNo",$data["parkingNo"])
                    ->where("clientNo",$data["clientNo"])
                    ->where("channelNo",$data["channelNo"])
                    ->find();
            if(!empty($re)){
                return "同一车场同一客户端不能设置两个相同的通道编号";
            }
           $res=Db::table("parking_camera")->insert($data);
           if($res){
               return "添加成功";
           }else{
               return "添加失败";
           }
       }else{
           $client_id=request()->get("id");
           
           $clientinfo=Db::table("parking_client")
                            ->alias("a")
                            ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                            ->where("a.id",$client_id)
                            ->field("a.*,b.parkingName")
                            ->find();
            //var_dump($clientinfo);               
           $this->assign("clientinfo",$clientinfo);
           return $this->fetch("camera_add");
       }
    }
    
    public function cameraEdit(){
       if(request()->isPost()){
           $data=request()->post();
           $res=Db::table("parking_camera")->update($data);
           if($res){
               return "修改成功";
           }else{
               return "修改失败";
           }
       }else{
           $id=request()->get("id");
           
           $info=Db::table("parking_camera")->where("id",$id)->find();
            //var_dump($clientinfo);               
           $this->assign("info",$info);
           return $this->fetch("camera_edit");
       }
    }
    public function cameraDel(){
        $id=request()->get("id");
        $res=Db::table("parking_camera")->where("id",$id)->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->success('删除失败');
        }
    }
    
}